#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <unistd.h>
#include <pthread.h>
#include "Thread.hpp"
#include <memory>

using namespace std;

// string changeId(const pthread_t &thread_id)
// {    
//     char tid[128];
//     snprintf(tid, sizeof(tid), "0x%x", thread_id);
//     return tid;
// }

// void *start_routine(void *args)
// {
//     string threadname = static_cast<const char*>(args);
//     int cnt = 5;
//     while(cnt--)
//     {
//         cout << threadname << " running ..." << changeId(pthread_self()) << endl;
//         sleep(1);
//     }
//     return nullptr;
// }

void *thread_run(void *args)
{
    std::string work_type = static_cast<const char*>(args);
    while(true)
    {
        std::cout << "我是一个新线程, 我正在做: " << work_type << std::endl;
        sleep(1);
    }
}

int main()
{
    std::unique_ptr<Thread> thread1(new Thread(thread_run, (void*)"hellothread", 1));
    std::unique_ptr<Thread> thread2(new Thread(thread_run, (void*)"countthread", 2));
    std::unique_ptr<Thread> thread3(new Thread(thread_run, (void*)"logthread", 3));
    // thread1->start();
    // thread2->start();
    // thread3->start();
    thread1->join();
    thread2->join();
    thread3->join();
    // pthread_t tid;
    // pthread_create(&tid, nullptr, start_routine, (void*)"thread one");
    
    // pthread_detach(pthread_self());
    
    // string main_id = changeId(pthread_self());
    // cout << "main thread running ... new thread id: " << changeId(tid) << " main thread id: " << main_id << endl;

    // sleep(5);
    // //int n = pthread_join(tid, nullptr);
    // //if(n < 0) cerr << "errno: " << errno << strerror(errno) << endl;
    // //cout << "result: " << n << strerror(n) << endl;
    return 0;
}